import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/user/login.vue'
import home from '../views/home/home.vue'
import qa from '../views/qa/qa.vue'
import movie from '../views/movie/movie.vue'
import user from '../views/user/user.vue'
import notfound from '../views/notfound/notfound.vue'
import edit from '../views/user/edit.vue'
import student from '../views/user/student.vue'
import { getToken } from '../utils/token'
import article from '../views/home/article.vue';
import search from "../views/home/search.vue";
import searchResult from "../views/home/searchResult.vue";

Vue.use(VueRouter)
    // import '@/utils/vant.js'
const routes = [{
        path: '/login',
        name: 'login',
        component: login
    }, {
        path: '/home',
        name: 'home',
        component: home,
        meta: { showTab: true }
    }, {
        path: '/qa',
        name: 'qa',
        component: qa,
        meta: { showTab: true }
    }, {
        path: '/movie',
        name: 'movie',
        component: movie,
        meta: { showTab: true }
    }, {
        path: '/user',
        name: 'user',
        component: user,
        meta: { showTab: true, isLogin: true }
    }, {
        path: '/notfound',
        name: 'notfound',
        component: notfound
    },
    {
        path: '/edit',
        name: 'edit',
        component: edit
    },
    {
        path: '/student',
        name: 'student',
        component: student
    },
    {

        path: '/article',
        name: 'myarticle',
        component: article
    },
    {
        path: '/search',
        name: 'search',
        component: search
    }, {
        path: '/searchResult',
        name: 'searchResult',
        component: searchResult
    },

    //路由的重定向
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '*',
        redirect: '/notfound'

    }

]


const router = new VueRouter({
    routes
})

//导航守卫
router.beforeEach((to, from, next) => {
    //判断是否要跳转到用户页面，如果是，就是true
    //判断token是否为undefined，如果是undefined,取反就是true
    if (to.meta.isLogin && !getToken()) {
        // alert('是否登录')
        next({ path: '/login' })
    } else {
        next()
    }
})
export default router